var app = getApp();
const quote = app.globalData.quote;
const path=quote.paths
const api = quote.api;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:null,
    path:path,
    isAuthorize:false,
    UID1:'??????',//未授权的时候显示
    UID:null,//授权后
    UCUID:null,
    introduction:'',//个人简介
    isEdit:false,//是否编辑状态
    num:0,
    isVIP:false,
    dengji:1,//用户等级
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   var that=this
   that.setData({
     introduction: '主人很懒，什么都没有留下~',//个人简介
   })

    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that=this
    that.getUserInfo()
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.userInfo)
    var that = this
    that.setData({
      userInfo: app.globalData.userInfo
    })
    console.log(that.data.userInfo)
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  //点击编辑
  edit:function(e){
    var that=this
    console.log(e)
    that.setData({
      isEdit: true
    })

  },


  //文本框输入事件
  getContent:function(e){
    console.log(e)
    var that=this;
    var num=e.detail.cursor;
    var introduction=e.detail.value;
    that.setData({
      num:num,
      introduction: introduction
    })
  },
  //未授权不能编辑
  gotoAuthorize:function(){
    wx.showModal({
      title: '暂未授权',
      content: '请前往授权中心授权后在编辑资料',
      success:function(res){
        if(res.confirm){
          wx.navigateTo({
            url: `/${path.authorize}`,
          })
        }
      },
      fail:(res)=>{
        console.log(res)
      }
    })
  },
  
  //获取用户信息
  getUserInfo: function (params, cb){
    var that=this
    var dataDic = {
      UCUID: app.globalData.UCUID
    }
    api.methodEnglishCorner_MyPersonalInformation({
      dataDic: dataDic,
      success: function (res) {
        console.log(res.data)
        if (res.data.code == 13) {
          console.log('该用户还没授权')
          // wx.showModal({
          //   title: '温馨提示',
          //   showCancel: false,
          //   content: res.data.msg || "数据请求失败",
          // })
          that.setData({
            isAuthorize:false
          })
          return;
        } else {
          that.setData({
            isAuthorize:true,
            UID:res.data.data.UID,
            dengji: res.data.data.Rank,
            isVIP:res.data.data.IsVip,
            introduction: res.data.data.Introduction,
            UCUID: res.data.data.UCUID
          })
        }
    
        typeof cb == 'function' && cb(res)
      },
      fail:function(res){
        console.log(res)
      }
    })

  },
  //保存编辑
  saveEdit: function (params, cb) {
    var that = this;
  
    var dataDic = {
      UCUID:that.data.UCUID,
      Introduction: that.data.introduction
    }
    api.methodEnglishCorner_UpPersonalInformation({
      dataDic: dataDic,
      complete: function (res) {
        console.log(res.data)
        if (res.data.code != 0) {
         wx.showToast({
           title: '修改资料失败',
         })
          return;
        } else {
          wx.showToast({
            title: '修改成功',
            icon:'success'
          })
          that.setData({
            isEdit: false
          })
        }
       
        typeof cb == 'function' && cb(res)
      }
    })
  },








})